const _toString = Object.prototype.toString

export function isFunction(obj) {
  return typeof obj === 'function' || false
}

export function isObject(obj) {
  return _toString.call(obj) === '[object Object]' || false
}

export function hasKey(key) {
  return key !== undefined
}

/**
 * 验证表单参数是否是空
 * @param  {...any} params
 */
export function verifyFormParams(...params) {
  params.forEach(param => {
    if (isUndefined(param)) {
      return false
    }
  })
  return true
}

// 成功显示消息提示框
export function success(msg) {
  wx.showToast({
    title: 'msg' + msg,
    icon: 'success',
    duration: 2000
  })
}

// 失败显示消息提示框
export function error(msg) {
  wx.showToast({
    title: 'msg:' + msg,
    icon: 'none',
    duration: 2000
  })
}

export function defaultValUndefined(val, defaultVal) {
  return val === undefined ? defaultVal : val
}

export function isUndefined(val) {
  return val === undefined
}

export function checkUndefined(val) {
  if (val === undefined) {
    console.error(val + ': 不能为 undefined')
  } else {
    return val
  }
}

/**
 * 判断字符串是否为空
 */
export function isEmpty(string) {
  return string === undefined || string.length === 0
}

/**
 * 以 date 为基准，返回 d 天后（前）日期，
 * date 默认为当前时间
 * @param {Number} d
 * @param {Date} date
 */
export function calculateDate(nDays, date) {
  date = isUndefined(date) ? new Date() : date
  var temp = new Date(date)
  temp.setDate(date.getDate() + nDays)
  return temp
}

/**
 *
 * 时间格式化为字符串
 * @param {Date} date
 */
export function getStringDate(date) {
  var d = defaultValUndefined(date, new Date())
  var year = d.getFullYear()
  var month = d.getMonth() + 1
  month = month < 10 ? '0' + month : month
  var day = d.getDate()
  day = day < 10 ? '0' + day : day
  return `${year}-${month}-${day}`
}
